package cn.edu.fudan.direct;

import cn.edu.fudan.data.HandelFeature;
import cn.edu.fudan.data.HandleDistance;
import cn.edu.fudan.data.SlideWindow;
import cn.edu.fudan.tools.GetConfig;
import cn.edu.fudan.type.Config;
import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.Params;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TwoClassClassifiers {
	private Logger logger = Logger.getLogger(TwoClassClassifiers.class);
	
	private List<DataItem> data;
	private List<List<Long>> timepoints;

	public TwoClassClassifiers(List<DataItem> data, List<List<Long>> timepoints) {
		this.data = data;
		this.timepoints = timepoints;
	}

	public double Classifier(Params params) {
		double accuracy = 0;
		Config config;
		try {
			config = new GetConfig().getConfig();
			HandelFeature handelFeature= new HandelFeature();
			SlideWindow slideWindow = new SlideWindow();
			HandleDistance handleDistance = new HandleDistance();
			
			List<List<DataItem>> data_A = slideWindow.extractWindow(data, timepoints.get(0),
					params.getWindow_length());
			List<List<DataItem>> data_B = slideWindow.extractWindow(data, timepoints.get(1),
					params.getWindow_length());
			
			
			List<List<Double>> maps_A = new ArrayList<>();
			List<List<Double>> maps_B = new ArrayList<>();

			for (int i = 0; i < data_A.size(); i++) {
				List<Double> map = handelFeature.handleFeature(data_A.get(i), params.getN_segment());
				maps_A.add(map);
			}

			for (int i = 0; i < data_B.size(); i++) {
				List<Double> map = handelFeature.handleFeature(data_B.get(i), params.getN_segment());
				maps_B.add(map);
			}		
			
			accuracy = handleDistance.calAccuracy(maps_A, maps_B, maps_A, maps_B, config.getK());   //这里统计的是正确率
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return accuracy;

	}

	public double PaaClassifier(Params params){  //用PAA分类器
		double accuracy = 0;
		Config config;
		try {
			config = new GetConfig().getConfig();
			HandelFeature handelFeature= new HandelFeature();
			SlideWindow slideWindow = new SlideWindow();
			HandleDistance handleDistance = new HandleDistance();

			List<List<DataItem>> data_A = slideWindow.extractWindow(data, timepoints.get(0),
					params.getWindow_length());
			List<List<DataItem>> data_B = slideWindow.extractWindow(data, timepoints.get(1),
					params.getWindow_length());


			List<List<Double>> maps_A = new ArrayList<>();
			List<List<Double>> maps_B = new ArrayList<>();

			for (int i = 0; i < data_A.size(); i++) {
				List<Double> map = handelFeature.handlePaaFeature(data_A.get(i), params.getN_segment());
				maps_A.add(map);
			}

			for (int i = 0; i < data_B.size(); i++) {
				List<Double> map = handelFeature.handlePaaFeature(data_B.get(i), params.getN_segment());
				maps_B.add(map);
			}

			accuracy = handleDistance.calAccuracy(maps_A, maps_B, maps_A, maps_B, config.getK());   //这里统计的是正确率

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return accuracy;
	}
}
